<?php

class InventoryClientsForm extends InventoryForm
{
    const FIELD_ID                     = 'id';
    const FIELD_NAME                   = 'name';
    const FIELD_BIRTHDAY               = 'birthday';
    const FIELD_PHONE_NUMBER           = 'phone_number';
    const FIELD_PHOTO_LINK             = 'photo_link';
    const FIELD_ACTION                 = 'action';

    public function Bind()
    {
        $this->values = array();
        $this->values[self::FIELD_NAME] = Request::GetParam(self::FIELD_NAME);
        $this->values[self::FIELD_BIRTHDAY] = Request::GetParam(self::FIELD_BIRTHDAY);
        $this->values[self::FIELD_PHONE_NUMBER] = Request::GetParam(self::FIELD_PHONE_NUMBER);
        $this->values[self::FIELD_PHOTO_LINK] = Request::GetParam(self::FIELD_PHOTO_LINK);
        $this->values[self::FIELD_ACTION] = Request::GetParam(self::FIELD_ACTION);
        $this->isValid = $this->Validate();
    }

    public function Save()
    {
        $itemManager = new InventoryClientsManager();
        $inventoryClient = new InventoryClient();
        $inventoryClient->SetName($this->GetValue(self::FIELD_NAME));
        $inventoryClient->SetBirthday($this->GetValue(self::FIELD_BIRTHDAY));
        $inventoryClient->SetPhoneNumber($this->GetValue(self::FIELD_PHONE_NUMBER));
        $inventoryClient->SetPhotoLink($this->GetValue(self::FIELD_PHOTO_LINK));

        $idField = Request::GetParam(self::FIELD_ID);
        if (!empty($idField) && ($this->values[self::FIELD_ACTION] == 'update'))
        {
            $inventoryClient->SetId($idField);
            $itemManager->Update($inventoryClient);
        }
        else
        {
            $itemManager->Insert($inventoryClient);
        }
    }

    protected function Validate()
    {
        if (empty($this->values[self::FIELD_NAME])) return false;
        return true;
    }

}